<?php

/*
 * WON AJAX
 * http://code.google.com/p/w-o-n/
 *
 * Copyright (c) 2009 WON
 * licensed under the MIT (MIT-LICENSE.txt)
 */

header("content-type:text/xml;charset=utf-8");
echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<maps>'."\n";

if(!empty($_GET['request'])) {
  
  $query = '';
  $ctime = time() - 86400;
  $type = $_GET['request'];
  if($type == 'new') {
    $query = "SELECT ts,lat,lng,cmt,typ,val,vna FROM wall_marks m, wall_types t, wall_value v WHERE lat != 0 && lng != 0 && m.tid = t.tid && m.vid = v.vid ORDER BY pid DESC LIMIT 200";
  } //get last 200 entries
    
  if($type == 'time') {
    $query = "SELECT ts,lat,lng,cmt,typ,val,vna FROM wall_marks m, wall_types t, wall_value v WHERE lat != 0 && lng != 0 && m.tid = t.tid && m.vid = v.vid && ts > ".$ctime." ORDER BY pid DESC LIMIT 200";
  } //get last 200 entries within 24hours
    
  if($type == 'affect') {
    $query = "SELECT ts,lat,lng,cmt,typ,val,vna FROM wall_marks m, wall_types t, wall_value v WHERE lat != 0 && lng != 0 && m.tid = t.tid && m.vid = v.vid && ts > ".$ctime." && m.vid != 1 ORDER BY pid DESC LIMIT 200";
  } //get last 200 entries within 24hours that's affected
    
  if(is_numeric($type) && $type < 100) {
    $query = "SELECT ts,lat,lng,cmt,typ,val,vna FROM wall_marks m, wall_types t, wall_value v WHERE m.tid = ".$type." && lat != 0 && lng != 0 && m.tid = t.tid && m.vid = v.vid ORDER BY pid DESC LIMIT 200";
  } //get last 200 entries of a certain type  
    
  if($type == 'service') {
    $query = "SELECT * FROM wall_types";
  } //get all types    
    
  if($query != '') {
    
	require("wallornot-config.php");
  
    mysql_query("SET NAMES 'utf8'"); //make sure it's unicode
    $result = mysql_query ($query, $connection);
  }

  if($result) {
    if($type == 'new' || $type ==  'time' || $type ==  'affect' || is_numeric($type)) {
      while($row = mysql_fetch_row($result)) {
      echo ' <marker>'."\n";
      echo '  <ts>'.$row[0].'</ts>'."\n";
      echo '  <lat>'.$row[1].'</lat>'."\n";
      echo '  <lng>'.$row[2].'</lng>'."\n";
      echo '  <cmt>'.htmlspecialchars(stripslashes($row[3])).'</cmt>'."\n";
      echo '  <typ>'.$row[4].'</typ>'."\n";
      echo '  <val>'.$row[5].'</val>'."\n";
      echo '  <vna>'.$row[6].'</vna>'."\n";
      echo ' </marker>'."\n";
      }
    }
    if($type == 'service') {
      while($row = mysql_fetch_row($result)) {
      echo ' <marker>'."\n";
      echo '  <tid>'.$row[0].'</tid>'."\n";
      echo '  <typ>'.$row[1].'</typ>'."\n";
      echo ' </marker>'."\n";
      }
    }
  } else {
    echo "<error>error detected</error>";
  }
  
//not enough data
} else {
  echo "<error>not enough data</error>"; 
}
echo '</maps>';
?>